#include<bits/stdc++.h>
#define int long long
using namespace std;
const int mod=999999999;
int a[4010][4010],tr[4010][8010],n,m,num,ans;
int sum(int x,int z){
	int res=0;
	for(int i=x;i>0;i-=(i&(-i)))res+=tr[z][i];
	return res;
}
void add(int x,int y,int z){for(int i=x;i<=n;i+=(i&(-i)))tr[z][i]+=y;}
signed main(){
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	freopen("fire.in","r",stdin);
	freopen("fire.out","w",stdout);
	cin>>n>>m;
	while(m--){
		int opt;
		cin>>opt;
		if(opt==1){
			int dir,id,op,x;
			cin>>dir>>id>>op>>x;
			if(dir==0){
				if(op==1)for(int i=1;i<=n;++i)num=(a[id][i]&x),add(i,num-a[id][i],id),a[id][i]=num;
				else if(op==2)for(int i=1;i<=n;++i)num=(a[id][i]|x),add(i,num-a[id][i],id),a[id][i]=num;
				else if(op==3)for(int i=1;i<=n;++i)num=(a[id][i]^x),add(i,num-a[id][i],id),a[id][i]=num;
				else for(int i=1;i<=n;++i)num=(a[id][i]+x)%mod,add(i,num-a[id][i],id),a[id][i]=num;
			}
			else{
				if(op==1)for(int i=1;i<=n;++i)num=(a[i][id]&x),add(id,num-a[i][id],i),a[i][id]=num;
				else if(op==2)for(int i=1;i<=n;++i)num=(a[i][id]|x),add(id,num-a[i][id],i),a[i][id]=num;
				else if(op==3)for(int i=1;i<=n;++i)num=(a[i][id]^x),add(id,num-a[i][id],i),a[i][id]=num;
				else for(int i=1;i<=n;++i)num=(a[i][id]+x)%mod,add(id,num-a[i][id],i),a[i][id]=num;
			}
		}
		else{
			int x,y;
			cin>>x>>y,ans=0;
			for(int i=1;i<=x;++i)ans+=sum(y,i);
			cout<<ans<<"\n";
		}
	}
	return 0;
}
